package com.xgg.taiwuinfo.gongfa.base;

import android.content.Context;
import android.graphics.Color;

import com.xgg.taiwuinfo.R;
import com.xgg.taiwuinfo.gongfa.abilities.Ability_YunGong_Action;
import com.xgg.taiwuinfo.gongfa.abilities.Ability_YunGong_Attack;
import com.xgg.taiwuinfo.gongfa.abilities.Ability_YunGong_Defend;
import com.xgg.taiwuinfo.gongfa.abilities.Ability_YunGong_HPandPoison;
import com.xgg.taiwuinfo.gongfa.abilities.Ability_YunGong_Heal;
import com.xgg.taiwuinfo.gongfa.abilities.Ability_YunGong_QiShi;
import com.xgg.taiwuinfo.gongfa.readingeffect.Reading;
import com.xgg.taiwuinfo.utils.Element;
import com.xgg.taiwuinfo.utils.Inherit;
import com.xgg.taiwuinfo.utils.Requirement;

import org.json.JSONArray;
import org.json.JSONObject;

//功法基类
public abstract class Gongfa {
    public final String mName;
    public final int mLevel;
    public final int mElement;
    public final int mType;
    public final int mInherit;
    public int mId;     //这个id描述在总队列中的位置，免得去遍历查找
    protected int mGridOccupy = 1;        //默认均占1格
    protected int[] mAttrRequired = new int[Requirement.ATTR_CNT];           //属性要求
    protected int[] mGongfaRequired = new int[Requirement.GF_CNT];        //功法资质造诣需求
    protected int[] mJiyiRequired = new int[Requirement.JY_CNT];         //技艺资质造诣需求

    public int getGridOccupy(){return mGridOccupy;}

    public void setId() { mId = GongfaList.mGongfaList.size(); }

    public int getId() { return mId; }

    static int[] mLevelColorArray = {Color.BLACK , Color.RED ,0xffffa500 , Color.YELLOW ,0xffff00ff ,Color.CYAN, 0xff8fbae7,Color.GREEN, Color.WHITE, Color.GRAY };
    public int getLevelColor(){return mLevelColorArray[mLevel]; }

    static int[] mElementColorArray = {Color.WHITE , Color.YELLOW , 0xffff00ff ,Color.GREEN, 0xff8fbae7,Color.RED };
    public int getElementColor() {return mElementColorArray[mElement] ;}

    public String getInheritString() { return  Inherit.mInheritStringArray[mInherit];}

    public String getElementString(Context ctx)
    {
        return Element.getElementString(ctx , mElement);
    }

    static public String getElementString(Context ctx , int element)
    {
        return Element.getElementString(ctx , element);
    }

    static public void InitConst(Context ctx)
    {
        Inherit.Init(ctx);
    }

    public Gongfa(String name , int level , int element , int type , int inherit)
    {
        mName = name;
        mLevel = level;
        mElement = element;
        mType = type;
        mInherit = inherit;
    }

    public int getType() { return mType; }
    public abstract String getTypeString(Context ctx);

    //获取运功字符串
    public String getYunGongString(Context ctx)
    {
        String rt = "";
        if (ability_yunGong_hpandpoison != null) {rt = rt + ability_yunGong_hpandpoison.getDescString(ctx); }
        if (ability_yunGong_defend != null){ rt = rt + ability_yunGong_defend.getDescString(ctx); }
        if (ability_yunGong_heal != null){ rt = rt + ability_yunGong_heal.getDescString(ctx); }
        if (ability_yunGong_action != null){ rt = rt + ability_yunGong_action.getDescString(ctx); }
        if (ability_yunGong_qishi != null){ rt = rt + ability_yunGong_qishi.getDescString(ctx); }
        if (ability_yunGong_attack != null){ rt = rt + ability_yunGong_attack.getDescString(ctx); }
        return  rt;
    }
    //运功效果
    Ability_YunGong_Defend ability_yunGong_defend = null;
    Ability_YunGong_HPandPoison ability_yunGong_hpandpoison = null;
    Ability_YunGong_Heal ability_yunGong_heal = null;
    Ability_YunGong_Action ability_yunGong_action = null;
    Ability_YunGong_QiShi ability_yunGong_qishi = null;
    Ability_YunGong_Attack ability_yunGong_attack = null;

    public Ability_YunGong_Action getAbility_yunGong_action() { return ability_yunGong_action; }
    public Ability_YunGong_Attack getAbility_yunGong_attack() { return ability_yunGong_attack; }

    //获取需求字符串
    public String getRequireStr()
    {
        String rt = "";
        for (int i = 0; i < Requirement.GF_CNT; i++)
        {
            if (mGongfaRequired[i] > 0)
            {
                String str = String.format("功法[%s] :  %d\r\n" , Requirement.getGongfaStr(i) , mGongfaRequired[i]);
                rt = rt + str;
            }
        }
        for (int i = 0; i < Requirement.ATTR_CNT; i++)
        {
            if (mAttrRequired[i] > 0)
            {
                String str = String.format("属性[%s] :  %d\r\n" , Requirement.getAttrStr(i) , mAttrRequired[i]);
                rt = rt + str;
            }
        }
        for (int i = 0; i < Requirement.JY_CNT; i++)
        {
            if (mJiyiRequired[i] > 0)
            {
                String str = String.format("技艺[%s] :  %d\r\n" , Requirement.getJiyiStr(i) , mJiyiRequired[i]);
                rt = rt + str;
            }
        }

        return rt;
    }

    //正逆练特效
    protected Reading PosfullReading = null;
    protected Reading NegfullReading = null;

    //读取正逆练特效
    public String getPosReadingString(Context ctx)
    {
        if (PosfullReading != null)
        {
            return PosfullReading.getString(ctx , this);
        }
        else
        {
            return ctx.getString(R.string.reading_none);
        }
    }

    public String getNegReadingString(Context ctx)
    {
        if (NegfullReading != null)
        {
            return NegfullReading.getString(ctx , this);
        }
        else
        {
            return ctx.getString(R.string.reading_none);
        }
    }

    private JSONArray mYunGongArray = new JSONArray();
    protected void PushYunGongObject(int field , int value)
    {
        try {
                JSONObject obj1 = new JSONObject();
                obj1.put("f", field);
                obj1.put("v", value);
                mYunGongArray.put(obj1);
        }
        catch (Exception e) {}
    }

    protected void InitYunGongAbility()
    {
        int [] action = new int[Ability_YunGong_Action.Count];
        boolean action_b = false;
        int [] attack = new int[Ability_YunGong_Attack.Count];
        boolean attack_b = false;
        int [] defend = new int[Ability_YunGong_Defend.Count];
        boolean defend_b = false;
        int [] heal = new int[Ability_YunGong_Heal.Count];
        boolean heal_b = false;
        int [] hp_poison = new int[Ability_YunGong_HPandPoison.Count];
        boolean hp_poison_b = false;
        int [] qishi = new int[Ability_YunGong_QiShi.Count];
        boolean qishi_b = false;
        try {
            for (int i = 0; i < mYunGongArray.length(); i++)
            {
                JSONObject abl = mYunGongArray.getJSONObject(i);
                int field = abl.getInt("f");
                int value = abl.getInt("v");
                switch (field)
                {
                    case ABL_INDEX_ShiZhanSuDu: action[0] = value;action_b = true; break;
                    case ABL_INDEX_GongJiSuDu: action[1] = value; action_b = true;break;
                    case ABL_INDEX_WuQiQieHuan: action[2] = value; action_b = true;break;
                    case ABL_INDEX_YiDongSuDu: action[3] = value; action_b = true;break;
                    case ABL_INDEX_YiDongJuLi: action[4] = value; action_b = true;break;

                    case ABL_INDEX_Lidao: attack[0] = value;attack_b=true; break;
                    case ABL_INDEX_Jingmiao: attack[1] = value; attack_b=true;break;
                    case ABL_INDEX_Xunji: attack[2] = value;attack_b=true; break;
                    case ABL_INDEX_LidaoFahui: attack[3] = value;attack_b=true; break;
                    case ABL_INDEX_JingmiaoFahui: attack[4] = value;attack_b=true; break;
                    case ABL_INDEX_XunjiFahui: attack[5] = value;attack_b=true; break;
                    case ABL_INDEX_WaiShang: attack[6] = value;attack_b=true; break;
                    case ABL_INDEX_NeiShang: attack[7] = value;attack_b=true; break;
                    case ABL_INDEX_PoTi: attack[8] = value;attack_b=true; break;
                    case ABL_INDEX_PoQi: attack[9] = value;attack_b=true; break;

                    case ABL_INDEX_Huti: defend[0] = value; defend_b=true;break;
                    case ABL_INDEX_HutiFahui : defend[3] = value;defend_b=true; break ;
                    case ABL_INDEX_Yuqi : defend[1] = value;defend_b=true; break ;
                    case ABL_INDEX_YuqiFahui : defend[4] = value;defend_b=true; break ;
                    case ABL_INDEX_Neixi : defend[2] = value;defend_b=true; break ;
                    case ABL_INDEX_NeixiFahui : defend[5] = value;defend_b=true; break ;
                    case ABL_INDEX_Xieli : defend[6] = value;defend_b=true; break ;
                    case ABL_INDEX_XieliFahui : defend[9] = value;defend_b=true; break ;
                    case ABL_INDEX_Chaizhao : defend[7] = value;defend_b=true; break ;
                    case ABL_INDEX_ChaizhaoFahui : defend[10] = value;defend_b=true; break ;
                    case ABL_INDEX_Shanbi : defend[8] = value;defend_b=true; break ;
                    case ABL_INDEX_ShanbiFahui : defend[11] = value;defend_b=true; break ;

                    case ABL_INDEX_ShouYu : heal[0] = value;heal_b= true; break ;
                    case ABL_INDEX_ZhiLiao : heal[1] = value;heal_b= true; break ;
                    case ABL_INDEX_QuDu : heal[2] = value;heal_b= true; break ;

                    case ABL_INDEX_AntiLie : hp_poison[0] = value;hp_poison_b= true; break ;
                    case ABL_INDEX_AntiYu : hp_poison[1] = value;hp_poison_b= true; break ;
                    case ABL_INDEX_AntiHan : hp_poison[2] = value;hp_poison_b= true; break ;
                    case ABL_INDEX_AntiChi : hp_poison[3] = value;hp_poison_b= true; break ;
                    case ABL_INDEX_AntiFu : hp_poison[4] = value;hp_poison_b= true; break ;
                    case ABL_INDEX_AntiHuan : hp_poison[5] = value;hp_poison_b= true; break ;
                    case ABL_INDEX_HPI : hp_poison[7] = value;hp_poison_b= true; break ;
                    case ABL_INDEX_HPO : hp_poison[6] = value;hp_poison_b= true; break ;

                    case ABL_INDEX_Tiqi : qishi[0] = value;qishi_b=true; break ;
                    case ABL_INDEX_Jiashi : qishi[1] = value;qishi_b=true; break ;
                    case ABL_INDEX_TiqiCost : qishi[2] = value;qishi_b=true; break ;
                    case ABL_INDEX_JiashiCost : qishi[3] = value;qishi_b=true; break ;
                    case ABL_INDEX_NeigongFahui : qishi[4] = value;qishi_b=true; break ;

                    default: break;
                }
            }   //end for
            if (action_b)
            {
                ability_yunGong_action = new Ability_YunGong_Action(action);
            }
            if (attack_b)
            {
                ability_yunGong_attack = new Ability_YunGong_Attack(attack);
            }
            if (defend_b)
            {
                ability_yunGong_defend = new Ability_YunGong_Defend(defend);
            }
            if (heal_b)
            {
                ability_yunGong_heal = new Ability_YunGong_Heal(heal);
            }
            if (hp_poison_b)
            {
                ability_yunGong_hpandpoison = new Ability_YunGong_HPandPoison(hp_poison);
            }
            if (qishi_b)
            {
                ability_yunGong_qishi = new Ability_YunGong_QiShi(qishi);
            }
        }
        catch (Exception e){
        }
    }

    public static final int ABL_INDEX_ShiZhanSuDu = 0;
    public static final int ABL_INDEX_GongJiSuDu = 1;
    public static final int ABL_INDEX_WuQiQieHuan = 2;
    public static final int ABL_INDEX_YiDongSuDu = 3;
    public static final int ABL_INDEX_YiDongJuLi = 4;

    public static final int ABL_INDEX_Lidao = 10;
    public static final int ABL_INDEX_Jingmiao = 11;
    public static final int ABL_INDEX_Xunji = 12;
    public static final int ABL_INDEX_LidaoFahui = 13;
    public static final int ABL_INDEX_JingmiaoFahui = 14;
    public static final int ABL_INDEX_XunjiFahui = 15;
    public static final int ABL_INDEX_WaiShang = 16;
    public static final int ABL_INDEX_NeiShang = 17;
    public static final int ABL_INDEX_PoTi = 18;
    public static final int ABL_INDEX_PoQi = 19;

    public static final int ABL_INDEX_Huti = 20;
    public static final int ABL_INDEX_HutiFahui = 21;
    public static final int ABL_INDEX_Yuqi = 22;
    public static final int ABL_INDEX_YuqiFahui = 23;
    public static final int ABL_INDEX_Neixi = 24;
    public static final int ABL_INDEX_NeixiFahui = 25;
    public static final int ABL_INDEX_Xieli = 26;
    public static final int ABL_INDEX_XieliFahui = 27;
    public static final int ABL_INDEX_Chaizhao = 28;
    public static final int ABL_INDEX_ChaizhaoFahui = 29;
    public static final int ABL_INDEX_Shanbi = 30;
    public static final int ABL_INDEX_ShanbiFahui = 31;

    public static final int ABL_INDEX_ShouYu = 40;
    public static final int ABL_INDEX_ZhiLiao = 41;
    public static final int ABL_INDEX_QuDu = 42;

    public static final int ABL_INDEX_AntiLie = 50;
    public static final int ABL_INDEX_AntiYu = 51;
    public static final int ABL_INDEX_AntiHan = 52;
    public static final int ABL_INDEX_AntiChi = 53;
    public static final int ABL_INDEX_AntiFu = 54;
    public static final int ABL_INDEX_AntiHuan = 55;
    public static final int ABL_INDEX_HPI = 56;
    public static final int ABL_INDEX_HPO = 57;

    public static final int ABL_INDEX_Tiqi = 60;
    public static final int ABL_INDEX_Jiashi = 61;
    public static final int ABL_INDEX_TiqiCost = 62;
    public static final int ABL_INDEX_JiashiCost = 63;
    public static final int ABL_INDEX_NeigongFahui = 64;
}
